package cn.com.controller.operationController;


import cn.com.Enum.UserAuth;
import cn.com.annotation.Auth;
import cn.com.constant.Code;
import cn.com.db.model.Surface;
import cn.com.db.model.StoreSurface;
import cn.com.db.service.SurfaceService;
import cn.com.db.service.StoreSurfaceService;
import cn.com.util.DataResult;
import cn.com.util.JsonUtil;
import cn.com.util.TimeUtil;
import cn.com.util.UidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@Auth(UserAuth.SELLER)
@RestController
@RequestMapping("/operation/surface")
public class SurfaceOperationController {

    @Autowired
    SurfaceService surfaceService;
    @Autowired
    StoreSurfaceService storeSurfaceService;

    @RequestMapping(method = RequestMethod.POST)
    public String getSurface(@RequestParam("storeId")String storeId,
                             @RequestParam(value = "parent") String parent){
        DataResult dataResult=new DataResult();
        dataResult.setData(storeSurfaceService.getStoreSurface(storeId,parent));
        dataResult.setTotal(storeSurfaceService.countStoreSurface(storeId,parent));
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("返回成功");
        return JsonUtil.toJsonString(dataResult);
    }


    @RequestMapping(value = "/onSurface",method = RequestMethod.POST)
    public String onSurface(@RequestParam("storeId") String storeId,
                         @RequestParam("surfaceId")String surfaceId,
                         @RequestParam("price")BigDecimal price){
        DataResult dataResult=new DataResult();
        if (!surfaceService.isExist(surfaceId)){
            dataResult.setCode(Code.NOT_FOUND);
            dataResult.setMessage("表层不存在");
            return JsonUtil.toJsonString(dataResult);
        }
        StoreSurface storeSurface=storeSurfaceService.getStoreSurfaceOfStore(storeId,surfaceId);
        Surface surface=surfaceService.getSurfaceById(surfaceId);
        if (storeSurface==null){
            storeSurface=new StoreSurface();
            storeSurface.setId(UidUtil.createUUID());
            storeSurface.setSurface(surface);
            storeSurface.setStore(storeId);
            storeSurface.setDel(false);
            storeSurface.setPrice(price);
            storeSurface.setParent(surface.getParent());
            storeSurface.setCreated_at(TimeUtil.getNowTimestamp());
            storeSurface.setUpdated_at(TimeUtil.getNowTimestamp());
            storeSurfaceService.onSurface(storeSurface);
            dataResult.setData(storeSurfaceService.getStoreSurfaceById(storeSurface.getId()));
        }else if (storeSurface.getDel()){
            storeSurface.setDel(false);
            storeSurface.setPrice(price);
            storeSurfaceService.updateSurface(storeSurface);
            dataResult.setData(storeSurfaceService.getStoreSurfaceById(storeSurface.getId()));
        }else {
            dataResult.setCode(Code.SERVER_ERROR);
            dataResult.setMessage("店铺已启用该表层");
            return JsonUtil.toJsonString(dataResult);
        }
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("操作成功");
        return JsonUtil.toJsonString(dataResult);
    }


    @RequestMapping(value = "/offSurface",method = RequestMethod.POST)
    public String offSurface(@RequestParam("id") String storeSurfaceId){
        DataResult dataResult=new DataResult();
        if (!storeSurfaceService.isAlive(storeSurfaceId)){
            dataResult.setCode(Code.SERVER_ERROR);
            dataResult.setMessage("表层不存在,或表层不处于启用状态");
            return JsonUtil.toJsonString(dataResult);
        }
        StoreSurface storeSurface= storeSurfaceService.getStoreSurfaceById(storeSurfaceId);
        storeSurface.setDel(true);
        storeSurfaceService.updateSurface(storeSurface);
        dataResult.setData(storeSurface);
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("操作成功");
        return JsonUtil.toJsonString(dataResult);
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public String offSurface(@RequestParam("id") String storeSurfaceId,
                          @RequestParam("price")BigDecimal price){
        DataResult dataResult=new DataResult();
        if (!storeSurfaceService.isAlive(storeSurfaceId)){
            dataResult.setCode(Code.SERVER_ERROR);
            dataResult.setMessage("表层不存在,或表层不处于启用状态");
            return JsonUtil.toJsonString(dataResult);
        }
        StoreSurface storeSurface= storeSurfaceService.getStoreSurfaceById(storeSurfaceId);
        storeSurface.setPrice(price);
        storeSurfaceService.updateSurface(storeSurface);
        dataResult.setData(storeSurface);
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("修改成功");
        return JsonUtil.toJsonString(dataResult);
    }

}
